﻿@using Inovout.Data.Domain;
@using Inovout.Data.Repository;
@model IDataObjectPagedSet
@if (Model.PageCount > 1)
{
    int startPage = 1, endPage = 10;
    //如果总页数小于结束页码
    if (Model.PageCount < endPage)
    {
        //则结束页码为总页数
        endPage = Model.PageCount;
    }
    else
    {
        //当前页大于5后开始重新计算起始页,否则起始页为1
        startPage = Model.PageIndex > 5 ? Model.PageIndex - 5 : startPage;
        //起始页码加9减去总页数,用于查看是否超过了总页数
        int result = startPage + 9 - Model.PageCount;
        //如果超过总页数肯定大于0
        if (result > 0)
        {
            endPage = Model.PageCount;//设置结束页码为总页数
            startPage = startPage - result;//超过最后页,补差
        }
        else
        {
            //不超过总页数,起始页加9计算出结束页码
            endPage = startPage + 9;
        }
    }

    <nav>
        <ul class="pagination">
            @if (startPage > 1)
            {
                if (Html.ViewData.GetQueryCriteria(Model) != null)
                {
                    <li>
                        @Html.ActionLink("«", Html.ViewContext.RouteData.GetRequiredString("action"),
                        QueryCriteriaHelper.BuildRouteDictionary(Html.ViewData.GetQueryCriteria(Model),
                       new RouteValueDictionary { { "ps", Model.PageSize }, { "pi", (startPage - 1) } }
                        ))
                    </li>
                }
                else
                {
                    <li>
                        @Html.RouteLink("«", new { ps = Model.PageSize, pi = (startPage - 1) });
                    </li>
                }
            }
            else
            {
                <li><span>&laquo;</span></li>
            }
            @for (int page = startPage; page <= endPage; page++)
            {
                if (page == Model.PageIndex)
                {
                    <li class="active"><span>@page<span class="sr-only">(current)</span></span></li>
                }
                else if (Html.ViewData.GetQueryCriteria(Model) != null)
                {
                    <li>
                        @Html.ActionLink(page.ToString(), Html.ViewContext.RouteData.GetRequiredString("action"),
                        QueryCriteriaHelper.BuildRouteDictionary(Html.ViewData.GetQueryCriteria(Model),
                       new RouteValueDictionary { { "ps", Model.PageSize }, { "pi", page } }
                        ))
                    </li>
                }
                else
                {
                    <li>
                        @Html.RouteLink(page.ToString(), new { ps = Model.PageSize, pi = page });
                    </li>
                }

            }
            @if (Model.PageIndex < Model.PageCount)
            {
                if (Html.ViewData.GetQueryCriteria(Model) != null)
                {
                    <li>
                        @Html.ActionLink("»", Html.ViewContext.RouteData.GetRequiredString("action"),
                        QueryCriteriaHelper.BuildRouteDictionary(Html.ViewData.GetQueryCriteria(Model),
                       new RouteValueDictionary { { "ps", Model.PageSize }, { "pi", (endPage + 1) } }
                        ))
                    </li>
                }
                else
                {
                    <li>
                        @Html.RouteLink("»", new { ps = Model.PageSize, pi = (1 + endPage) });
                    </li>
                }
            }
            else
            {
                <li class="disabled" aria-label="Previous"><span aria-hidden="true">&raquo;</span></li>
            }
            <li>
                <span>每页：@Model.PageSize 条</span>
                <span>总记录数：@Model.RecordCount 条</span>
         <span>总页数：@Model.PageCount 页</span>
            </li>
        </ul>
    </nav>
}
